﻿using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.EventBroker;
using NetROS.Tiers.Entities;
using NetRosApp.FinPart.Constants;
using NetRosApp.Infrastructure.Interface.Base;
using DevExpress.XtraReports.UI;
using ReportiFin;
using NetRosApp.Infrastructure.Interface;
using NetROS.Tiers.Services;
using NetRosApp.Infrastructure.Interface.Base.BaseViews;
using System.Drawing.Printing;


namespace NetRosApp.FinPart
{
    public static class OpenIzvjestaj
    {
        public static void ShowReportForEntity(Object sender, Object entitet, WorkItem WorkItem)
        {
            XtraReport report = null;
            ReportZagPodnozje _reportZagPodnozje = null;
            string naziv = string.Empty;
            BindingSource _bindingSource = null;
            ReportSelectorFrm _tmpfrm;
            System.Collections.ArrayList _tmpList;

            if (entitet is ObracunKamata)
            {
                ObracunKamata _entity = entitet as ObracunKamata;
                if (_entity.IDOrganizacijaSource == null || _entity.IDOrganizacija != _entity.IDOrganizacijaSource.ID)
                {
                    _entity.IDOrganizacijaSource =
                        WorkItem.Services.Get<OrganizacijaService>().GetByID(_entity.IDOrganizacija);
                }
                if (_entity.IDOrganizacijaSource != null)
                {
                    _entity.IDOrganizacijaSource.OrganizacijaZiroRacunCollection =
                        WorkItem.Services.Get<OrganizacijaZiroRacunService>().GetByIDOrganizacija(_entity.IDOrganizacija);
                }
                if (_entity.IDKlijentSource == null || _entity.IDKlijent != _entity.IDKlijentSource.ID)
                {
                    _entity.IDKlijentSource = WorkItem.Services.Get<KlijentService>().GetByID(_entity.IDKlijent);
                }
                if (_entity.IDKlijentSource != null && _entity.IDKlijentSource.IDMjestoSource == null)
                {
                    _entity.IDKlijentSource.IDMjestoSource =
                        WorkItem.Services.Get<MjestoService>().GetByID(_entity.IDKlijentSource.IDMjesto);
                }
                if (_entity.IDOrganizacijaSource != null && _entity.IDOrganizacijaSource.IDMjestoSource == null &&
                    _entity.IDOrganizacijaSource.IDMjesto.HasValue)
                {
                    _entity.IDOrganizacijaSource.IDMjestoSource =
                        WorkItem.Services.Get<MjestoService>().GetByID(_entity.IDOrganizacijaSource.IDMjesto.Value);
                }
                if (_entity.ObracunKamataStavkeCollection == null ||
                    (_entity.ObracunKamataStavkeCollection.Count == 0 &&
                     _entity.ObracunKamataStavkeCollection.DeletedItems.Count == 0))
                {
                    _entity.ObracunKamataStavkeCollection =
                        WorkItem.Services.Get<ObracunKamataStavkeService>().GetByIDObracunKamata(_entity.ID);
                }
                report = new ObracunKamataReport();
                report.Name = string.Format("Obračun kamata {0}", _entity.Oznaka);
                _bindingSource = report.DataSource as BindingSource;
                if (_bindingSource != null)
                    _bindingSource.DataSource = _entity;
                _reportZagPodnozje = new ReportZagPodnozje(String.Empty);
                _reportZagPodnozje.xrSubreport1.ReportSource = report;
                _reportZagPodnozje.Name = string.Format("Obračun kamata {0}", _entity.Oznaka);
                naziv = string.Format("Obračun kamata {0}", _entity.Oznaka);
            }

            if (sender is VirmaniViewPresenter)
            {
                _tmpfrm = new ReportSelectorFrm();
                _tmpList = new System.Collections.ArrayList();
                _tmpList.Add("Lista virmana");
                _tmpList.Add("Lista virmana 2");
                _tmpList.Add("Lista virmana 3");
                _tmpList.Add("Virman");
                _tmpList.Add("Ispis virmana na A4 predložak");
                
                _tmpfrm.LBIzabir.DataSource = _tmpList;
                _tmpfrm.LBIzabir.SelectedIndex = 0;
                if (_tmpfrm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Lista virmana")
                    {
                        report = new VirmaniIzvj();
                        report.Name = string.Format("Lista virmana");
                        _bindingSource = report.DataSource as BindingSource;
                        if (_bindingSource != null)
                            _bindingSource.DataSource = entitet;
                        _reportZagPodnozje = new ReportZagPodnozje(String.Empty);
                        _reportZagPodnozje.xrSubreport1.ReportSource = report;
                        _reportZagPodnozje.Name = string.Format("Lista virmana");
                        naziv = string.Format("Lista virmana");
                    }

                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Lista virmana 2")
                    {
                        report = new VirmaniIspis2();
                        ((VirmaniIspis2)report).xrLabelTvrtka.Text = (string)WorkItem.RootWorkItem.Items["CompanyName"];

                        report.Name = string.Format("Lista virmana");
                        _bindingSource = report.DataSource as BindingSource;
                        if (_bindingSource != null)
                            _bindingSource.DataSource = entitet;
                        naziv = string.Format("Lista virmana");
                        report.Landscape = true;
                        
                    }

                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Lista virmana 3")
                    {
                        report = new VirmaniIspis3();
                        ((VirmaniIspis3)report).xrLabelTvrtka.Text = (string)WorkItem.RootWorkItem.Items["CompanyName"];

                        report.Name = string.Format("Lista virmana");
                        _bindingSource = report.DataSource as BindingSource;
                        if (_bindingSource != null)
                            _bindingSource.DataSource = entitet;
                        naziv = string.Format("Lista virmana");

                    }

                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Virman")
                    {
                        report = new VirmaniIspis1();
                        report.Name = string.Format("Virman");
                        _bindingSource = report.DataSource as BindingSource;
                        if (_bindingSource != null)
                            _bindingSource.DataSource = entitet;
                        naziv = string.Format("Virman");
                        
                    }
                    
                    if (_tmpfrm.LBIzabir.SelectedItem.ToString() == "Ispis virmana na A4 predložak")
                    {
                        report = new VirmaniIspis4();
                        report.Name = string.Format("Virman");
                        _bindingSource = report.DataSource as BindingSource;
                        if (_bindingSource != null)
                            _bindingSource.DataSource = entitet;
                        naziv = string.Format("Virman");
                        WorkItem.EventTopics[EventTopicNames.PrikaziIzvjestajTag].Fire(sender, new EventArgs<object, string,string>(report, naziv,"CustomSizes"), WorkItem, PublicationScope.Global);
                        return;

                    }


                }
            }

            if (sender is KnjizenjeTraziPresenter || sender is KnjizenjeViewPresenter)
            {
                report = new KnjizenjeIspis();
                report.Name = string.Format("Ispis knjiženja");
                ((KnjizenjeIspis)report).xltvrtka.Text = (String) WorkItem.RootWorkItem.Items["CompanyName"];
                if (entitet is VList<VwKnjizenjeIspis> && ((VList<VwKnjizenjeIspis>) entitet).Count > 0 &&
                    WorkItem.Services.Get<KnjizenjeService>().GetByID(((VList<VwKnjizenjeIspis>) entitet)[0].IDKnjiz)
                        .IDStatus == 0)
                {
                    report.Watermark.Text = "P R I V R E M E N I";
                    report.Watermark.ShowBehind = true;
                }

                _bindingSource = report.DataSource as BindingSource;
                if (_bindingSource != null)
                    _bindingSource.DataSource = entitet;
                //_reportZagPodnozje = new ReportZagPodnozje("","");
                //_reportZagPodnozje.xrSubreport1.ReportSource = report;
            
            //    _reportZagPodnozje.Name = string.Format("Ispis knjiženja");
                naziv = string.Format("Ispis knjiženja");
            }

    if (report != null)
            {
                if (_reportZagPodnozje != null)
                {
                    WorkItem.EventTopics[EventTopicNames.PrikaziIzvjestaj].Fire(sender, new EventArgs<object, string>(_reportZagPodnozje, naziv), WorkItem, PublicationScope.Global);
                }
                else 
                {
                    WorkItem.EventTopics[EventTopicNames.PrikaziIzvjestaj].Fire(sender, new EventArgs<object, string>(report, naziv), WorkItem, PublicationScope.Global);
                }
            }
        }
    }
}
